import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;



public class SerializationPBServer extends Thread {
	private boolean throughputTest = true;
	private final int port;
	private static final int OBJECTS_COUNT = 10_000;

	public SerializationPBServer(int port) {
		this.port = port;
	}

	public static void main(String[] args) {
		if (args.length < 1) {
			System.err.println("Invalid parameters list. Usage: SerializationJSONServer <port> ");
			System.exit(1);
		}
		int port = Integer.parseInt(args[0]);
		SerializationPBServer server = new SerializationPBServer(port);
		server.start();
	}

	public void run() {
		
		try (ServerSocket serverSocket = new ServerSocket(port);
				Socket socket = serverSocket.accept()){
			DataOutputStream out = new DataOutputStream(socket.getOutputStream());
			out.flush();
			InputStream in = socket.getInputStream();
			for (int i = 0; i < OBJECTS_COUNT; i++) {
				SensorDataProto.SensorData2 sd2 = SensorDataProto.SensorData2.parseDelimitedFrom(in);
				if (!throughputTest) {
					out.writeByte(1);
					out.flush();
				}
			}
			out.writeByte(0);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
